package start

import (
	"github.com/gorilla/mux"
	"log"
	"src/api/http/controller/admin"
	"src/api/http/controller/user"
	"src/config"
	"src/handler"
	service2 "src/service"
)

// 启动http服务
func HttpRouter() *mux.Router {
	//contextPath := config.V().GetString("server.http.servlet.context-path")
	http1 := mux.NewRouter()
	//http1 := http.NewServeMux()
	isOpen := config.V().GetBool("server.http.user-api.isOpen")
	if isOpen {
		(&RouterGroup{Name: "user", Path: "/user"}).
			//模拟登录 返回jwt token
			Add((&Router{Path: "/login", Run: user.NewUserC().Login})).
			// 模拟注册
			Add((&Router{Path: "/reg", Run: user.NewUserC().Reg})).
			Reg(http1)
		////模拟登录 返回jwt token
		//(&Router{Path:  "/user/login", Run: user.NewUserC().Login}).Reg(http1)
		//// 模拟注册
		//(&Router{Path:  "/user/reg", Run: user.NewUserC().Reg}).Reg(http1)
		// 模拟获取用户信息 	//这行代码意思:http访问 Path 地址触发执行 Run 控制器，在Run被触发前按顺序执行OnBefore 里面的方法，before里面的方法调用req.next(req)是继续，否则是终止
		(&RouterGroup{Name: "user", Path: "/user", OnBefore: []func(*service2.Req){(&service2.Req{}).Auth}}).
			// 获取用户信息
			Add((&Router{Path: "/getUserinfo", Run: user.NewUserC().GetUserinfo})).
			// 获取用户列表
			Add((&Router{Path: "/getList", Run: user.NewUserC().GetList})).
			// 模拟获取消息列表
			Add((&Router{Path: "/message/getList", Run: user.NewMessageC().GetList})).
			// 模拟发送消息
			Add((&Router{Path: "/message/send", Run: user.NewMessageC().Send})).
			Reg(http1)
		(&RouterGroup{Name: "message", Path: "/message", OnBefore: []func(*service2.Req){(&service2.Req{}).Auth}}).
			// 模拟获取消息列表
			Add((&Router{Path: "/getList", Run: user.NewMessageC().GetList})).
			// 模拟发送消息
			Add((&Router{Path: "/send", Run: user.NewMessageC().Send})).
			Reg(http1)
	}
	//下面是留给外部应用调用的接口 需要控制安全 防止外网访问
	(&RouterGroup{Name: "user", Path: "/user"}).
		// 模拟通过http请求绑定用户cid 和uid关系
		Add(&Router{Path: "/bindUser", Run: admin.NewBind().BindUser}).
		// 通过http请求绑定用户uid绑定RoomNo 房间号关系
		Add(&Router{Path: "/bindRoom", Run: admin.NewBind().BindRoom}).
		// 通过http请求 用户退出房间
		Add(&Router{Path: "/outRoom", Run: admin.NewBind().OutRoom}).
		Reg(http1)

	(&RouterGroup{Name: "admin", Path: "/admin"}).
		// 通过http请求获得房间下的所有用户uid
		Add(&Router{Path: "/getRoomAllUser", Run: admin.NewAdmin().GetRoomAllUser}).

		// 通过http请求获得房间下的所有用户uid
		Add(&Router{Path: "/getAllRoom", Run: admin.NewAdmin().GetAllRoom}).
		//查看在线人信息
		Add(&Router{Path: "/getOnlineUser", Run: admin.NewAdmin().GetOnlineUser}).
		Reg(http1)

	(&RouterGroup{Name: "send", Path: "/send"}).
		//推送一条消息
		Add(&Router{Path: "/toUid", Run: admin.NewMessage().Send}).
		//给多个用户推送 一条消息
		Add(&Router{Path: "/toUids", Run: admin.NewMessage().SendUids}).
		//给用户的某个在线ID推一条消息
		Add(&Router{Path: "/toUidDid", Run: admin.NewMessage().SendUidDid}).
		//推一组消息
		Add(&Router{Path: "/sendList", Run: admin.NewMessage().SendList}).
		//推给房间内所有人
		Add(&Router{Path: "/toRoom", Run: admin.NewMessage().SendRoom}).
		//推送消息给所有人
		Add(&Router{Path: "/all", Run: admin.NewMessage().SendAll}).
		Reg(http1)
	return http1
}

// 启动websocket服务
func WebSocketRouter() *mux.Router {
	contextPath := config.V().GetString("server.websocket.servlet.context-path")
	http1 := mux.NewRouter()
	(&Router{Path: contextPath, Run: handler.NewWebSocketHandler().RequestHandler}).Reg(http1)
	(&Router{Path: "/{act}/{value}", Run: handler.NewWebSocketHandler().RequestHandler}).Reg(http1)
	//Reg(&Router{Path: "/ws/{token}/{roomNo}", Run: service.NewWebSocketHandler().RequestHandler}))
	//监听websocket服务端口
	//log.Fatal(http.ListenAndServe(ip, http1))
	return http1
}

// 启动TCP服务
func TcpRouter() *handler.TcpHandler {
	log.Println("TcpStart\n")
	//http1 := http.NewServeMux()
	//Reg(&Router{Path: "/ws", Run: service.NewWebSocketHandler().RequestHandler}))
	//监听websocket服务端口
	//handler.NewTcpHandler().TcpHandler(ip, port)
	return handler.NewTcpHandler()
}
